import React, { useEffect } from 'react';
import dayjs from 'dayjs';
import NavBar from '../components/NavBar/NavBar';
import { Button } from 'antd-mobile';

const Houseprice = () => {
  const handleButtonClick = () => {
    window.electron.ipcRenderer.sendMessage(
      'start-fetching-houseprice-data',
      [],
    );
  };

  useEffect(() => {
    const unsubscribe = window.electron.ipcRenderer.on(
      'houseprice-data-fetching-complete',
      (e: any, data: any) => {
        console.log('Event:', e);
        console.log('Data:', data);
      },
    );

    return () => {
      unsubscribe();
    };
  }, []);

  // useEffect(() => {
  //   const now = dayjs();
  //   let targetTime = now.hour(15).minute(9).second(0).millisecond(0);
  //   let tomorrow;

  //   if (now.isAfter(targetTime)) {
  //     tomorrow = targetTime.add(1, 'day');
  //   }

  //   const msUntilTarget = tomorrow ? tomorrow.diff(now) : targetTime.diff(now);

  //   const timeOutId = window.setTimeout(() => {
  //     console.log('Scheduled data fetch triggered');
  //     window.electron.ipcRenderer.sendMessage('start-fetching-houseprice-data', []);
  //   }, msUntilTarget);

  //   return () => clearTimeout(timeOutId);
  // }, []);

  return (
    <div>
      <NavBar />
      <Button block color="primary" size="large" onClick={handleButtonClick}>
        下载房价数据到本地
      </Button>
    </div>
  );
};

export default Houseprice;
